#import "OmGiftsListVC.h"
#import "MJExtension.h"
#import "OmPersonGiftsCell.h"
#import "OmGiftModel.h"
@interface OmGiftsListVC ()<UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewRefreshDelegate>
@property (nonatomic, strong) UICollectionView *listView;
@end
@implementation OmGiftsListVC
- (void)viewDidLoad {
    [super viewDidLoad];
}
- (void)configSelf {
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = ASLocalizedString(@"礼物");
     [self setHiddenForTabBar:YES navigationBar:NO];
}
- (void)refreshSelf {
    [self.listView.mj_header beginRefreshing];
}
#pragma mark - EventGraph
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.listView.refreshDataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    OmPersonGiftsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kOmPersonGiftsCell forIndexPath:indexPath];
    [cell setupContent:self.listView.refreshDataArray[indexPath.item]];
    return cell;
}
- (void)scrollView:(UIScrollView *)scrollView didBeginRefresh:(UIScrollView *(^)(NSMutableArray *))refreshData {
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"user_api" A:@"mygift"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"page"] = @(self.listView.refreshPage);
    [CYNET POSTv3:url parameters:param responseCache:^(id responseObject) {
    } success:^(id responseObject) {
        NSDictionary *data = responseObject[@"data"];
        NSMutableArray *modelList = [OmGiftModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"data"]];
        refreshData(modelList);
    } failure:^(NSString *error, NSInteger code) {
    } hasCache:YES];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
}
- (void)allocateComponents {
    self.listView = (UICollectionView *)[[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[self getCollectionViewLayout]]
    .fp_dataSource(self)
    .fp_delegate(self)
    .fp_enableRefreshAndSetRefreshDelegate(self)
    .fp_refreshPageCount(10)
    .fp_backgroundColor([UIColor qmui_colorWithHexString:@"#F7F7F7"]);
    [self.listView registerNib:[UINib nibWithNibName:kOmPersonGiftsCell bundle:nil] forCellWithReuseIdentifier:kOmPersonGiftsCell];
    [self.view addSubview:self.listView];
}
- (void)layoutComponents {
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
}
- (UICollectionViewFlowLayout *)getCollectionViewLayout {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat marginWidth = RPX(15);
    CGFloat itemWidth = (SCREEN_WIDTH - marginWidth * 4) / 3.0;
    CGFloat itemHeight = itemWidth + 25;
    layout.itemSize =CGSizeMake(itemWidth, itemHeight);
    layout.minimumInteritemSpacing = marginWidth;
    layout.minimumLineSpacing = RPX(20);;
    layout.sectionInset = UIEdgeInsetsMake(RPX(20), marginWidth, 0, marginWidth);
    return layout;
}
@end
